package com.station.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.station.manage.dao.MenuMapper;
import com.station.manage.model.Menu;
import com.station.manage.model.dto.AddMenuDto;
import com.station.manage.model.dto.UpdateMenuDto;
import com.station.manage.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * 菜单服务类
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 添加菜单
     * 对于上级菜单的添加若无则添加-1，有则先根据名字查再添加其id
     * @param addMenuDto
     * @return
     */
    @Override
    public int insert(AddMenuDto addMenuDto) {

        Menu menu = new Menu();
        BeanUtils.copyProperties(addMenuDto,menu);
        String pValue = addMenuDto.getPvalue();
        if(pValue.equals("无")){
            menu.setParent(-1L);
        }else {
            Menu byName = menuMapper.findByName(addMenuDto.getPvalue());
            menu.setParent(byName.getMenuid());
        }
        return menuMapper.insert(menu);
    }

    /**
     * 修改菜单
     * @param updateMenuDto
     * @return
     */
    @Override
    public int update(UpdateMenuDto updateMenuDto) {

        Menu menu = new Menu();
        BeanUtils.copyProperties(updateMenuDto,menu);
        String pValue = updateMenuDto.getPvalue();
        if(pValue.equals("无")){
            menu.setParent(-1L);
        }else {
            Menu byName = menuMapper.findByName(updateMenuDto.getPvalue());
            menu.setParent(byName.getMenuid());
        }
        if(updateMenuDto.getHideva().equals("是")){
            menu.setIshide(1);
        }
        else{
            menu.setIshide(0);
        }

        return menuMapper.update(menu);
    }

    /**
     * 删除菜单
     * @param menuid
     * @return
     */
    @Override
    public int delete(Long menuid) {
        return menuMapper.delete(menuid);
    }

    /**
     * 分页查找菜单
     * @param page
     * @param limit
     * @return
     */

    @Override
    public PageInfo <Menu> findAll(int page,int limit) {
        PageHelper.startPage(page-1,limit);
        return  new PageInfo <Menu>(menuMapper.findAll());
    }

    /**
     * 根据目录查找菜单
     * @param pvalue
     * @return
     */
    @Override
    public List<Menu> findByParent(String pvalue) {
        Menu byName = menuMapper.findByName(pvalue);
        return menuMapper.findByParent(byName.getParent());
    }

    /**
     * 根据id查找
     * @param menuid
     * @return
     */
    @Override
    public Menu findById(Long menuid) {
        return menuMapper.findById(menuid);
    }

    /**
     * 查找所有的目录
     * @return
     */
    @Override
    public List<Menu> findParent() {
        return menuMapper.findParent();
    }

    /**
     * 查询未隐藏的目录和菜单，用于做功能显示
     * @return
     */
    @Override
    public List<Menu> findNotHide() {
        return findNotHide();
    }
}
